package org.jeecg.modules.xxgx.fpht.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public final static String YYYY_MM_DD = "yyyy-MM-dd";

    public final static String  N_YYYY_MM_DD_HH_MM_SS= "yyyy-MM-dd HH:mm:ss";

    public final static String  YYYYMMDDHHMMSS= "yyyyMMddHHmmss";

    public final static String  YYYYMMDD= "yyyyMMdd";

    public final static String  YYYYMM= "yyyyMM";


    public final static String YYYY_MM_DD1 = "yyyy-MM-dd";

    private final static String YYYY_MM_DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss";

    /**
     * 当前日期
     * @return
     */
    public static Date now(){
        return Calendar.getInstance().getTime();
    }

    /**
     * 当前日期
     * @return
     */
    public static String nows(){
        return getFormatDate(now(),N_YYYY_MM_DD_HH_MM_SS);
    }

    public static String getCurrentDate(){
        return getFormatDate(now(),YYYY_MM_DD);
    }

    public static String getCurrentMonth(){
        return getFormatDate(now(),YYYYMM);
    }

    /**
     * 比较日期大小，返回差额数量
     * @param s 开始日期
     * @param e 结束日期
     * @return
     */
    public static int compare(Date s,Date e){
        return compare(s,e,YYYYMMDDHHMMSS);
    }

    /**
     * 比较日期大小，返回差额数量
     * @param s 开始日期
     * @param e 结束日期
     * @param pattern 日期格式
     * @return
     */
    public static int compare(Date s,Date e,String pattern){
        if(s == null || e == null){
            return -1;
        }
        return (Integer.parseInt(getFormatDate(e,pattern)) - Integer.parseInt(getFormatDate(s,pattern)));
    }

    //过去一年
    public static String getLastYear(){
        SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        return dateFormat.format(c.getTime());
    }

    public static Date parseDate(String s,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(s);
        }catch (Exception e) {
        }
        return null;
    }

    public static Date parseDate(String s){
        return parseDate(s,N_YYYY_MM_DD_HH_MM_SS);
    }

    public static String getFormatDate(Date date){
        return getFormatDate(date,YYYY_MM_DD);
    }

    public static String getFormatDate(Date date,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static Date strToDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        try {
            Date date = sdf.parse(dateStr);
            return date;
        }catch (ParseException e) {

        }
        return null;
    }

    public static Date strToDateByPa(String dateStr,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date date = sdf.parse(dateStr);
            return date;
        }catch (ParseException e) {

        }
        return null;
    }

    public static Date ymdStrToDate(String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        try {
            Date date = sdf.parse(dateStr);
            return date;
        }catch (ParseException e) {

        }
        return null;
    }

    public static Date getStartTime(Date date) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }


    public static Date getEndTime(Date date) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.SECOND, 59);
        todayStart.set(Calendar.MINUTE, 59);
        todayStart.set(Calendar.HOUR_OF_DAY, 23);
        todayStart.set(Calendar.MILLISECOND, 999);
        return todayStart.getTime();
    }

    public static boolean validateDateFormat(String date){
        return validateDateFormat(date,YYYY_MM_DD);
    }

    public static boolean validateDateFormat(String date,String format){
        if(date == null){
            return false;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try{
            Date d = sdf.parse(date);
            return date.equals(sdf.format(d));
        }catch(Exception e){
            return false;
        }
    }
    //获取上一个月 上月为-1，本月为0，下月为1，以此类推
    public static String getLastMonth(){
        return getFormatDate(ymdStrToDate(getMonthFirstDay(-1)),YYYYMM);
    }
    /**
     * 获取某月第一天，上月为-1，本月为0，下月为1，以此类推
     *
     * @return
     */
    public static String getMonthFirstDay(int monthBetweenToday) {
        return LocalDate.now()
                .minusMonths(-1L * monthBetweenToday)
                .withDayOfMonth(1).toString();
    }

    public static String getLastMonthDate(){
        String lastMonthDate = "";
        try{
            Calendar calendar = Calendar.getInstance();
            // 获取当前日期
            Date currentDate = new Date();
            calendar.setTime(currentDate);
            // 将日期设置到上一个月
            calendar.add(Calendar.MONTH, -1);
            // 转换成 yyyyMM 格式字符串
            SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
            lastMonthDate = format.format(calendar.getTime());
        }catch (Exception e){
            e.printStackTrace();
        }
        return lastMonthDate;
    }

}
